using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class PostDal : Base
    {
        public PostDal() : base() { }

        public enum Fields
        {
            Id,
            SiteId,
            SiteName,
            VersionId,
            ParentId,
            ParentPath,
            Name,
            DisplayName,
            TemplateId,
            TemplateName,
            ThumbnailId,
            Summary,
            Tags,
            Sequence,
            PostCreated,
            VersionCreated,
            StartDate,
            EndDate
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<int>(Fields.VersionId, reader, table);
            GetReaderValue<string>(Fields.SiteName, reader, table);
            GetReaderValue<int?>(Fields.ParentId, reader, table);
            GetReaderValue<string>(Fields.ParentPath, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.DisplayName, reader, table);
            GetReaderValue<int>(Fields.TemplateId, reader, table);
            GetReaderValue<string>(Fields.TemplateName, reader, table);
            GetReaderValue<int?>(Fields.ThumbnailId, reader, table);
            GetReaderValue<string>(Fields.Summary, reader, table);
            GetReaderValue<string>(Fields.Tags, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
            GetReaderValue<DateTime>(Fields.PostCreated, reader, table);
            GetReaderValue<DateTime>(Fields.VersionCreated, reader, table);
            GetReaderValue<DateTime?>(Fields.StartDate, reader, table);
            GetReaderValue<DateTime?>(Fields.EndDate, reader, table);
        }

        public Hashtable GetTopLevelBySiteId(int siteId, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetTopLevelBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetTopLevelBySiteIdAndMenuId(int siteId, int menuId, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetTopLevelByMenuIdAndSiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteIdMenuIdAndParentPath(int siteId, int menuId, string parentPath, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteIdMenuIdAndParentPath");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentPath", parentPath, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllBySiteIdParentPath(int siteId, string parentPath, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteIdMenuIdAndParentPath");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentPath", parentPath, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByMenuIdAndParentId(int menuId, int parentId, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetAllByMenuIdAndParentId");
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByParentId(int parentId, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetAllByParentId");
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetBySiteUrlPostUrlAndVersionTimeStamp(string siteUrl, string postUrl, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetBySiteUrlPostUrlAndVersionTimeStamp");
            AddInputParameter(cmd, "SiteUrl", siteUrl, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "PostUrl", postUrl, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdAndUrl(int siteId, string url, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdAndUrl");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Url", url, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdMenuIdAndUrl(int siteId, int menuId, string url, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdMenuIdAndUrl");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "MenuId", menuId, SqlDbType.Int);
            AddInputParameter(cmd, "Url", url, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByVersionId(int versionId)
        {
            SqlCommand cmd = CreateCommand("GetByVersionId");
            AddInputParameter(cmd, "VersionId", versionId, SqlDbType.Int);

            return ExecuteGetItem(cmd);
        }

        public Hashtable GetById(int id, DateTime versionTimeStamp)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);

            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllFutureVersionsById(int id)
        {
            SqlCommand cmd = CreateCommand("GetAllFutureVersionsById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);

            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int siteId, ref string siteName, ref int versionId, int? parentId, ref string parentPath, string name, string displayName, int templateId, ref string templateName, int? thumbnailId, string summary, string tags, ref int sequence, DateTime postCreated, DateTime versionCreated, DateTime? startDate, DateTime? endDate, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", versionId, SqlDbType.Int);
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "DisplayName", displayName, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            AddInputParameter(cmd, "ThumbnailId", thumbnailId, SqlDbType.Int);
            AddInputParameter(cmd, "Summary", summary, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Tags", tags, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Sequence", sequence, SqlDbType.Int);
            AddInputParameter(cmd, "PostCreated", postCreated, SqlDbType.DateTime);
            AddInputParameter(cmd, "VersionCreated", versionCreated, SqlDbType.DateTime);
            AddInputParameter(cmd, "StartDate", startDate, SqlDbType.DateTime);
            AddInputParameter(cmd, "EndDate", endDate, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "SiteNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ParentPathOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "TemplateNameOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "SequenceOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            siteName = GetOutputValue<string>(cmd, "SiteNameOut");
            versionId = GetOutputValue<int>(cmd, "VersionIdOut");
            parentPath = GetOutputValue<string>(cmd, "ParentPathOut");
            templateName = GetOutputValue<string>(cmd, "TemplateNameOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");

        }

        public void Delete(int id, object concurancyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurancyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

        public Hashtable[] GetAllByParentIdAndTopicId(int parentId, int topicId, DateTime versionTimeStamp, bool includeChildren, int startIndex, int pageSize, ref int totalRecords)
        {
            SqlCommand cmd = CreateCommand("GetAllByParentIdAndTopicId");
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "TopicId", topicId, SqlDbType.Int);
            AddInputParameter(cmd, "VersionTimeStamp", versionTimeStamp, SqlDbType.DateTime);
            AddInputParameter(cmd, "IncludeChildren", includeChildren, SqlDbType.Bit);
            return ExecuteGetPagedCollection(cmd, startIndex, pageSize, ref totalRecords);
        }
    }
}
